// pages/fenxiao/myPerformance.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
//获取应用实例
var app = getApp();
var cusmallToken = wx.getStorageSync('cusmallToken');
var Zan = require('../../../youzan/dist/index');
var baseHandle = require("../../template/baseHandle.js");
// pages/fenxiao/myInfo.js
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    app, app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    detailList: [],
    limit: 10,
    panelTab: {
      "selectedId": "0",
      "list": [
        { "id": "0", "title": "全部" },
        { "id": "1", "title": "已结算" },
        { "id": "2", "title": "冻结中" }
      ]
    },
    freezePop: false,
    hideDateModal: true,
    btEndDate: "",
    defBtEndDate: "",
    btStartDate: "",
    defBtStartDate: "",
    mSDate: "",
    mEDate: ""
  },

  handlePanelTabChange(e) {
    let vm = this;
    let selectedId = e.target.dataset.id;
    vm.setData({
      [`panelTab.selectedId`]: selectedId
    });
    vm.setData({
      detailList: [],
      total: 0
    })
    vm.fetchListData(true);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      var year = new Date().getFullYear() + "";
      var month = (new Date().getMonth() + 1) + "";
      var curMonth = year + "-" + month;
      if (options.month) {
        curMonth = options.month;
        if (options.month == "all"){
          curMonth = "";
        }
      }
      that.setData({
        mSDate:curMonth,
        btStartDate:curMonth
      })
      if(options.tabId){
        that.setData({
          [`panelTab.selectedId`]: options.tabId
        });
      }

      that.fetchListData(true);
    });
  },

  formatNumber: function (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  formatTime: function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    return [month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute].map(this.formatNumber).join(':')
  },


  fetchListData: function (isReload) {
    let vm = this;
    if (vm.data.loadingListData) {
      return;
    }
    wx.showLoading({
      title: "加载中",
    })
    let submitData = {
      cusmallToken: cusmallToken,
      start: isReload?0:vm.data.detailList.length,
      limit: vm.data.limit
    };
    if (vm.data.panelTab.selectedId == 1) {
      submitData.state = 1;
    } else if (vm.data.panelTab.selectedId == 2) {
      submitData.state = 0;
    }
    if (vm.data.btStartDate){
      submitData.month = vm.data.btStartDate.replace("-","");
    }
    vm.data.loadingListData = true;
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/salesperformance/queryCommissionDetails',
      data: submitData,
      header: {
        "content": "application/json"
      },
      success: function (res) {
        console.log(res.data);
        wx.hideLoading();
        vm.data.loadingListData = false;
        if (res.data.ret == 0) {
          let detailList = res.data.model.records;
          for (let i = 0; i < detailList.length; i++) {
            detailList[i].createTime = vm.formatTime(new Date(detailList[i].createTime));
          }
          if (isReload){
            vm.setData({
              detailList: []
            })
            vm.setData({ "detailList": detailList});
          } else {
            vm.setData({ "detailList": vm.data.detailList.concat(detailList) });
          }
          vm.setData({ "total": res.data.model.total });
          if (res.data.model.total == 0) {
            vm.setData({ "nomore": false });
            vm.setData({ "nodata": true });
          } else {
            vm.setData({ "nodata": false });
            if (vm.data.detailList.length >= res.data.model.total) {
              vm.setData({ "nomore": true });
            } else {
              vm.setData({ "nomore": false });
            }
          }
        }
      }
    })
  },

  showTips: function () {
    this.setData({
      freezePop: !this.data.freezePop
    });
  },

  handleScrollToLower: function () {

  },
  defResetDate() {
    var ctx = this;
    ctx.setData({
      btEndDate: ctx.data.defBtEndDate,
      btStartDate: ctx.data.defBtStartDate,
      curPage: 1,
      list: [],
      isBottom: false
    });
    ctx.fetchListData(true);
  },
  showDateModal() {
    var ctx = this;
    ctx.setData({
      hideDateModal: false,
      mSDate: ctx.data.btStartDate
    })
  },
  hideDateModal() {
    this.setData({
      hideDateModal: true
    })
  },
  bindDateChange(e) {
    console.log(e)
    var op = e.currentTarget.dataset.op;
    if ("start" == op) {
      this.setData({
        mSDate: e.detail.value
      });
    } else if ("end" == op) {
      this.setData({
        mEDate: e.detail.value
      });
    }
  },

  fetchBehTra(e) {
    var ctx = this;
    ctx.handleCommonFormSubmit(e);
    ctx.setData({
      hideDateModal: false,
      btStartDate: ctx.data.mSDate,
      btEndDate: ctx.data.mEDate,
      curPage: 1,
      list: [],
      isBottom: false
    });
    ctx.fetchListData(true);
    ctx.hideDateModal();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.detailList.length < that.data.total) {
      that.fetchListData();
    }
  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {

  }
}))